package com.finger.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.finger.commonutils.R;
import com.finger.eduservice.entity.EduCourse;
import com.finger.eduservice.entity.EduTeacher;
import com.finger.eduservice.entity.vo.CourseInfoVO;
import com.finger.eduservice.entity.vo.CoursePublishVO;
import com.finger.eduservice.entity.vo.QueryCourse;
import com.finger.eduservice.entity.vo.QueryTeacher;
import com.finger.eduservice.service.EduCourseService;
import com.finger.eduservice.service.EduSubjectService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-01-06
 */
@Log4j2
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    /**
     * 添加课程和课程详细信息
     * @param courseInfoVO
     * @return
     */
    @PostMapping("/addCourseAndDescription")
    public R addCourseAndDescription(@RequestBody CourseInfoVO courseInfoVO) {

        String courseId = eduCourseService.addCourseAndDescription(courseInfoVO);

        log.info(courseInfoVO);

        return R.ok().data("courseId", courseId);
    }

    /**
     * 根据课程ID返回CourseInfoVO
     * @param courseId
     * @return
     */
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfoVO(@PathVariable("courseId") String courseId){

        CourseInfoVO courseInfoVO = eduCourseService.getCourseInfo(courseId);

        return R.ok().data("courseInfo",courseInfoVO);
    }

    /**
     * 更新CourseInfo信息
     * @param courseInfoVO
     * @return
     */
    @PutMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVO courseInfoVO){

        eduCourseService.updateCourseInfo(courseInfoVO);

        return R.ok().data("courseId",courseInfoVO.getId());
    }

    /**
     * 根据courseId获取 CoursePublishVO 对象
     * @param courseId
     * @return
     */
    @GetMapping("/getCoursePublish/{courseId}")
    public R getCoursePublish(@PathVariable("courseId") String courseId){
        CoursePublishVO coursePublishVO = eduCourseService.getCoursePublish(courseId);

        return R.ok().data("coursePublish",coursePublishVO);
    }

    /**
     * 发布课程
     * @param courseId 课程ID
     * @return
     */
    @PutMapping("/publishCourse/{courseId}")
    public R  publishCourse(@PathVariable String courseId){

        eduCourseService.publishCourse(courseId);
        return R.ok();
    }

    /**
     * 条件查询分页
     * @param queryCourse
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/queryCourseCondition/{page}/{size}")
    public R queryTeacherByCondition(@RequestBody(required = false) QueryCourse queryCourse,
                                     @PathVariable(value = "page") Long page,
                                     @PathVariable(value = "size") Long size) {
        String name = queryCourse.getTitle();

        String status = queryCourse.getStatus();

        String begin = queryCourse.getBegin();

        String end = queryCourse.getEnd();

        QueryWrapper<EduCourse> pageWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(name)) {
            pageWrapper.like("title", name);
        }
        if (!StringUtils.isEmpty(status)) {
            pageWrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(begin)) {
            pageWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            pageWrapper.le("gmt_modified", end);
        }

        //排序
        pageWrapper.orderByDesc("gmt_create");

        Page<EduCourse> eduCoursePage = new Page<>(page, size);

        eduCourseService.page(eduCoursePage, pageWrapper);

        return R.ok().data("page",eduCoursePage);
    }

    @DeleteMapping("/deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){

        eduCourseService.removeCourseById(courseId);

        return R.ok();
    }

}

